import { useClipboard } from '@vueuse/core'
import useElMessage from "./useElMessage"

const { isSupported, copy } = useClipboard()

export default (value) => {
    let timer = null
    if (isSupported.value) {
        copy(value)
        timer = setTimeout(() => {
            useElMessage.success("复制成功")
        }, 10);
    } else {
        timer = setTimeout(() => {
            useElMessage.error("复制失败")
        }, 10);
    }

    const _then = (func) => {
        clearTimeout(timer)
        return new Promise((resolve, reject) => {
            clearTimeout(timer)
            if (isSupported.value) {
                if (func) func(value)
                resolve()
            }
            else reject("复制失败")
        })
    }

    const _catch = (func) => {
        clearTimeout(timer)
        return new Promise((resolve) => {
            clearTimeout(timer)
            if (func) func(isSupported.value ? "" : "复制失败")
            resolve()
        })
    }

    const _finally = (func) => {
        clearTimeout(timer)
        if (func) func()
    }

    return { then: _then, catch: _catch, finally: _finally }
}